﻿using Newtonsoft.Json;
 
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SMSSendTest
{
    public class WeatherSmsService
    {
        // private static readonly string apiUrl = "http://106.74.72.240:8000/umcinterface/sendtempletmsg";

        private static readonly string apiUrl = "http://rcsapi.wo.cn:8000/umcinterface/sendtempletmsg";
        public static string GetMd5Hash(string input)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                // 计算字符串的哈希值  
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 创建一个StringBuilder来收集字节  
                // 并创建字符串（十六进制格式）  
                StringBuilder sBuilder = new StringBuilder();

                // 循环遍历哈希数据的每个字节  
                // 并格式化为十六进制字符串  
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回十六进制字符串  
                return sBuilder.ToString();
            }
        }

        public static string SendSmsAsync(string phoneNumber, string message)
        {
            try
            {
                var data = new
                {
                    key = "d7063fcc2f1857868b8892423171a056",
                    cpcode = "AAAYHG",
                    msg = message,
                    mobiles = phoneNumber,
                    excode = "",
                    templetid = "198600"
                };

                var parameters = new Dictionary<string, string>();
                parameters.Add("cpcode", data.cpcode);
                parameters.Add("key", data.key);
                parameters.Add("msg", message);
                parameters.Add("mobiles", phoneNumber);
                parameters.Add("excode", "");
                parameters.Add("templetid", "198600");
                parameters.Add("sign", GetMd5Hash(data.cpcode + data.msg + data.mobiles + data.excode + data.templetid + data.key));


                string result1 = null;// WebRequestHelper.sendPost(apiUrl, parameters, "post");

            }
            catch (Exception ex)
            {
                // 捕获并处理异常  
                Console.WriteLine($"发送短信时发生错误: {ex.Message}");
                
            }
            return null;
        }


    }
}
